//
//  UserAccountViewModel.swift
//  SinaWeibo
//
//  Created by 梁楠 on 16/1/17.
//  Copyright © 2016年 QQ主流框架. All rights reserved.
//

import UIKit
import AFNetworking

class UserAccountViewModel: NSObject {

    var userAccount : UserAccount?
    
    override init() {
        userAccount = UserAccount.loadUserAccount()
        super.init()
    }
    
    var userLogin : Bool {
        return userAccount?.access_token != nil
    }
    
    var token : String? {
        return userAccount?.access_token
    }
    
    var userName : String? {
        return userAccount?.name
    }
    
    var headImageURL : NSURL? {
        return NSURL(string: userAccount?.avatar_large ?? "")
    }
    
    func loadAccessToken(code:String , finished : (isSuccess : Bool) -> ()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":redirect_uri]
        
        let AFN = AFHTTPSessionManager()
        
        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        AFN.POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            
            print(result)
            
            //将anyObject对象 转换成 [Key : Value]
            guard let dict =  result as? [String : AnyObject] else {
                
                print("数据格式不合法")
                
                finished(isSuccess: false)
                return
            }
            
            //            let access_token = dict["access_token"] as! String
            //            let uid = dict["uid"] as! String
            let account = UserAccount(dict: dict)
            //            print(account)
            
            self.loadUserInfo(account, finished: finished)
            
            }) { (_, error) -> Void in
                
                print(error)
                finished(isSuccess: false)
        }
        
        
        
    }
    
    //MARK 加载用户信息
    private func loadUserInfo (account : UserAccount , finished : (isSuccess : Bool) -> ()) {
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        //OC中的空值 {"nullobject" : [NSNull null]}
        let parameters = ["access_token":account.access_token!,"uid":account.uid!]
        
        let AFN = AFHTTPSessionManager()
        
        AFN.GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            
            //            print(result)
            
            guard let dict = result as? [String : AnyObject] else {
                
                print("格式不合法")
                finished(isSuccess: false)
                return
            }
            
            let name = dict["name"] as! String
            
            let avatar_large = dict["avatar_large"] as! String
            
            account.name = name
            account.avatar_large = avatar_large
            //            print(account)
            account.saveAccount()
            finished(isSuccess: true)
            
            }) { (_, error) -> Void in
                
                finished(isSuccess: false)
                //                print(error)
        }
        
    }
}
